package com.keon.jianbao.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.keon.jianbao.AppContext;
import com.keon.jianbao.AppManager;
import com.keon.jianbao.R;
import com.keon.jianbao.interf.BaseViewInterface;
import com.keon.jianbao.interf.DialogControl;
import com.keon.jianbao.util.DialogHelp;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by keon on 2015/7/13.
 */
public abstract class BaseActivity extends AppCompatActivity implements BaseViewInterface, DialogControl, View.OnClickListener {
    private boolean _isVisible;
    private ProgressDialog _waitDialog;
    //    protected ActionBar mActionBar;
    @Bind(R.id.toolbar)
    protected Toolbar toolbar;
    @Bind(R.id.toolbarTitle)
    TextView toolbarTitle;
    @Bind(R.id.title_left)
    LinearLayout vLeft;
    @Bind(R.id.title_right)
    LinearLayout vRight;
    @Bind(R.id.toolbarTab)
    LinearLayout llTab;
    @Bind(R.id.toolbar_title_tab1)
    protected TextView tvTab1;
    @Bind(R.id.toolbar_title_tab2)
    protected TextView tvTab2;

    private static String lastToast = "";
    private static long lastToastTime;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AppManager.getAppManager().addActivity(this);

        if (getLayoutId() != 0) {
            setContentView(getLayoutId());
        }

        ButterKnife.bind(this);
        init(savedInstanceState);
        initView();
        initData();

/*        mActionBar = getSupportActionBar();
        if (hasActionBar()) {
            initActionBar(mActionBar);
        }*/
        if (hasToolBar()) {
            initToolbar();
        } else {
            toolbar.setVisibility(View.GONE);
        }
        _isVisible = true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;

            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    protected void initToolbar() {
        if (toolbar == null)
            return;

        int titleRes = getToolBarTitle();
        if (titleRes != 0) {
            toolbar.setTitle("");
            toolbarTitle.setText(titleRes);
        }
        setSupportActionBar(toolbar);
        int h = toolbar.getHeight();
        if (hasBackButton()) {
            getSupportActionBar().setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        } else {

        }
    }

    protected void setToolBarTab(int tab1, int tab2) {
        toolbarTitle.setVisibility(View.GONE);
        llTab.setVisibility(View.VISIBLE);
        tvTab1.setText(tab1);
        tvTab1.setOnClickListener(this);
        tvTab2.setText(tab2);
        tvTab2.setOnClickListener(this);
    }

    protected void removeToolBarTab() {
        llTab.setVisibility(View.GONE);
    }

    protected void setRightView(int id) {
        LayoutInflater inflater = LayoutInflater.from(this);
        View v = inflater.inflate(id, null);
        setRightView(v);
    }

    protected void setRightView(View v) {
        vRight.setVisibility(View.VISIBLE);
        v.setOnClickListener(this);
        vRight.addView(v);
    }

    public void chooseToolbarTab(int id) {
        if (id == R.id.toolbar_title_tab1) {
            tvTab1.setTextAppearance(this, R.style.toolbarTabPressed);
            tvTab1.setBackgroundResource(R.drawable.toolbar_title_tab_pressed_bg);
            tvTab2.setTextAppearance(this, R.style.toolbarTab);
            tvTab2.setBackgroundResource(R.drawable.toolbar_title_bg);
        } else if (id == R.id.toolbar_title_tab2) {
            tvTab2.setTextAppearance(this, R.style.toolbarTabPressed);
            tvTab2.setBackgroundResource(R.drawable.toolbar_title_tab_pressed_bg);
            tvTab1.setTextAppearance(this, R.style.toolbarTab);
            tvTab1.setBackgroundResource(R.drawable.toolbar_title_bg);
        }
    }

    protected void setLeftView(int id) {
        vLeft.setVisibility(View.VISIBLE);
        LayoutInflater inflater = LayoutInflater.from(this);
        View v = inflater.inflate(id, null);
        v.setOnClickListener(this);
        vLeft.addView(v);
    }

    protected void removeRightView() {
        vRight.setVisibility(View.GONE);
        vRight.removeAllViews();
    }

    protected void removeLeftView() {
        vLeft.setVisibility(View.GONE);
        vLeft.removeAllViews();
    }

    protected void hideToolBar() {
        toolbar.setVisibility(View.GONE);
    }

    protected void showToolBar() {
        toolbar.setVisibility(View.VISIBLE);
    }

    public void setTitle(String s) {
        toolbarTitle.setText(s);
    }

    protected int getToolBarTitle() {
        return R.string.app_name;
    }

//    public void showToast(String message, int icon, int gravity) {
//        CommonToast toast = new CommonToast(this);
//        toast.setMessage(message);
//        toast.setMessageIc(icon);
//        toast.setLayoutGravity(gravity);
//        toast.show();
//    }
//
//    public void showToast(int msgResid, int icon, int gravity) {
//        showToast(getString(msgResid), icon, gravity);
//    }

    public static String string(int id) {
        return AppContext.getResource().getString(id);
    }

    public static String string(int id, Object... args) {
        return AppContext.getResource().getString(id, args);
    }

    public static void showToast(int message) {
        showToast(message, Toast.LENGTH_LONG, 0);
    }

    public static void showToast(String message) {
        showToast(message, Toast.LENGTH_LONG, 0, Gravity.BOTTOM);
    }

    public static void showToast(int message, int icon) {
        showToast(message, Toast.LENGTH_LONG, icon);
    }

    public static void showToast(String message, int icon) {
        showToast(message, Toast.LENGTH_LONG, icon, Gravity.BOTTOM);
    }

    public static void showToastShort(int message) {
        showToast(message, Toast.LENGTH_SHORT, 0);
    }

    public static void showToastShort(String message) {
        showToast(message, Toast.LENGTH_SHORT, 0, Gravity.BOTTOM);
    }

    public static void showToastShort(int message, Object... args) {
        showToast(message, Toast.LENGTH_SHORT, 0, Gravity.BOTTOM, args);
    }

    public static void showToast(int message, int duration, int icon) {
        showToast(message, duration, icon, Gravity.BOTTOM);
    }

    public static void showToast(int message, int duration, int icon,
                                 int gravity) {
        showToast(AppContext.getContext().getString(message), duration, icon, gravity);
    }

    public static void showToast(int message, int duration, int icon,
                                 int gravity, Object... args) {
        showToast(AppContext.getContext().getString(message, args), duration, icon, gravity);
    }

    public static void showToast(String message, int duration, int icon,
                                 int gravity) {
        if (message != null && !message.equalsIgnoreCase("")) {
            long time = System.currentTimeMillis();
            if (!message.equalsIgnoreCase(lastToast)
                    || Math.abs(time - lastToastTime) > 2000) {
                View view = LayoutInflater.from(AppContext.getContext()).inflate(
                        R.layout.view_toast, null);
                ((TextView) view.findViewById(R.id.title_tv)).setText(message);
                if (icon != 0) {
                    ((ImageView) view.findViewById(R.id.icon_iv))
                            .setImageResource(icon);
                    ((ImageView) view.findViewById(R.id.icon_iv))
                            .setVisibility(View.VISIBLE);
                }
                Toast toast = new Toast(AppContext.getContext());
                toast.setView(view);
                if (gravity == Gravity.CENTER) {
                    toast.setGravity(gravity, 0, 0);
                } else {
                    toast.setGravity(gravity, 0, 35);
                }

                toast.setDuration(duration);
                toast.show();
                lastToast = message;
                lastToastTime = System.currentTimeMillis();
            }
        }
    }

    public void jump(Class to) {
        jump(this, to);
    }

    public void jump(Context from, Class to) {
        jump(from, to, null);
    }

    public void jump(Class to, Bundle bundle) {
        jump(this, to, bundle);
    }

    public void jump(Context from, Class to, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(from, to);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        //设置切换动画，从右边进入，左边退出,带动态效果
        overridePendingTransition(R.anim.common_out_to_left, R.anim.common_in_from_right);
    }

    public void jumpForResult(Context from, Class to, Bundle bundle, int requestCode) {
        Intent intent = new Intent();
        intent.setClass(from, to);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
        //设置切换动画，从右边进入，左边退出,带动态效果
        overridePendingTransition(R.anim.common_out_to_left, R.anim.common_in_from_right);
    }

    public void jumpForResult(Context from, Class to, int requestCode) {
        jumpForResult(from, to, null, requestCode);
    }

    public void jumpForResult(Class to, int requestCode) {
        jumpForResult(this, to, null, requestCode);
    }

    public void jumpForResult(Class to, Bundle bundle, int requestCode) {
        jumpForResult(this, to, bundle, requestCode);
    }

    @Override
    public void onBackPressed() {
        AppManager.getAppManager().finishActivity();
        overridePendingTransition(R.anim.common_in_from_left, R.anim.common_out_to_right);
//        super.onBackPressed();
    }

    protected int getLayoutId() {
        return 0;
    }

    protected boolean hasToolBar() {
        return true;
    }

    protected boolean hasBackButton() {
        return true;
    }

    protected void init(Bundle savedInstanceState) {
    }

    @Override
    public ProgressDialog showWaitDialog() {
        return showWaitDialog(R.string.loading);
    }

    @Override
    public ProgressDialog showWaitDialog(int resid) {
        return showWaitDialog(getString(resid));
    }

    @Override
    public ProgressDialog showWaitDialog(String message) {
        if (_isVisible) {
            if (_waitDialog == null) {
                _waitDialog = DialogHelp.getWaitDialog(this, message);
            }
            if (_waitDialog != null) {
                _waitDialog.setMessage(message);
                _waitDialog.show();
            }
            return _waitDialog;
        }
        return null;
    }

    @Override
    public void hideWaitDialog() {
        if (_isVisible && _waitDialog != null) {
            try {
                _waitDialog.dismiss();
                _waitDialog = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}